/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.auth.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wf.captcha.SpecCaptcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.celery.modules.shop.entity.Shop;
import org.celery.modules.shop.entity.ShopUser;
import org.celery.modules.shop.service.IShopService;
import org.celery.modules.shop.service.IShopUserService;
import org.springblade.common.cache.CacheNames;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.AuthInfo;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.*;
import org.springblade.modules.auth.granter.ITokenGranter;
import org.springblade.modules.auth.granter.TokenGranterBuilder;
import org.springblade.modules.auth.granter.TokenParameter;
import org.springblade.modules.auth.utils.TokenUtil;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.entity.UserInfo;
import org.springblade.modules.system.service.IUserService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 认证模块
 *
 * @author Chill
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-auth")
@Api(value = "用户授权认证", tags = "授权接口")
public class AuthController {

	private RedisUtil redisUtil;
	private IUserService userService;
	private final IShopService shopService;

	@PostMapping("token")
	@Transactional(rollbackFor = Exception.class)
	@ApiOperation(value = "获取认证token", notes = "传入租户ID:tenantId,账号:account,密码:password")
	public R<AuthInfo> token(@ApiParam(value = "授权类型", required = true) @RequestParam(defaultValue = "password", required = false) String grantType,
							 @ApiParam(value = "刷新令牌") @RequestParam(required = false) String refreshToken,
							 @ApiParam(value = "租户ID", required = true) @RequestParam(defaultValue = "000000", required = false) String tenantId,
							 @ApiParam(value = "账号") @RequestParam(required = false) String account,
							 @ApiParam(value = "密码") @RequestParam(required = false) String password,
							 @ApiParam(value = "aliId") @RequestParam(required = false) String aliId) {

		String userType = Func.toStr(WebUtil.getRequest().getHeader(TokenUtil.USER_TYPE_HEADER_KEY), TokenUtil.DEFAULT_USER_TYPE);

		// 获取tenantId
		List<User> userList = userService.list(Wrappers.<User>lambdaQuery().eq(User::getAccount, account));
		if (Func.isNotEmpty(userList)) {
			tenantId = userList.get(0).getTenantId();
		}

		TokenParameter tokenParameter = new TokenParameter();
		tokenParameter.getArgs().set("tenantId", tenantId)
			.set("account", account)
			.set("password", password)
			.set("grantType", grantType)
			.set("refreshToken", refreshToken)
			.set("userType", userType);

		ITokenGranter granter = TokenGranterBuilder.getGranter(grantType);
		UserInfo userInfo = granter.grant(tokenParameter);

		if (userInfo.getUser().getStatus().equals(2)) {
			return R.fail(TokenUtil.USER_DISABLED);
		}

		// 存入redis,user角色
		redisUtil.set("userInfo", userInfo.getUser());
		if (userInfo == null || userInfo.getUser() == null) {
			return R.fail(TokenUtil.USER_NOT_FOUND);
		}

		if (Func.isEmpty(aliId)) {
			return R.data(TokenUtil.createAuthInfo(userInfo));
		}

		Shop shop = shopService.getOne(Wrappers.<Shop>lambdaQuery()
				.eq(Shop::getAliId, aliId)
		);

		if (Func.isEmpty(shop)) {
			throw new ServiceException("店铺信息未创建!");
		}

		// 设置店铺关联的tenantId
		shop.setTenantId(tenantId);
		shopService.saveOrUpdate(shop);

		// 给这个用户添加一条关联信息
		if (!userInfo.getRoles().contains("administrator") || !userInfo.getRoles().contains("s_admin")) {
			IShopUserService shopUserService = SpringUtil.getBean(IShopUserService.class);
			List<ShopUser> shopUserList = shopUserService.list(Wrappers.<ShopUser>lambdaQuery().eq(ShopUser::getShopId, shop.getId()).eq(ShopUser::getUserId, userInfo.getUser().getId()));
			if (Func.isEmpty(shopUserList)) {
				ShopUser shopUser = new ShopUser();
				shopUser.setShopId(shop.getId());
				shopUser.setUserId(userInfo.getUser().getId());
				shopUserService.save(shopUser);
			}
		}

		return R.data(TokenUtil.createAuthInfo(userInfo));
	}

	@GetMapping("/captcha")
	@ApiOperation(value = "获取验证码")
	public R<Kv> captcha() {
		SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
		String verCode = specCaptcha.text().toLowerCase();
		String key = UUID.randomUUID().toString();
		// 存入redis并设置过期时间为30分钟
		redisUtil.set(CacheNames.CAPTCHA_KEY + key, verCode, 30L, TimeUnit.MINUTES);
		// 将key和base64返回给前端
		return R.data(Kv.init().set("key", key).set("image", specCaptcha.toBase64()));
	}

}
